<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */

namespace App\Content\Repositories;

use App\Content\Contracts\ArticleInterface;
use App\Content\Exceptions\ContentException;
use App\Content\Models\AdvModel;
use App\Content\Models\ArticleClassModel;
use App\Content\Models\ArticleModel;
use App\Content\Models\InfoCollectionModel;
use App\Content\Models\InfoCommentsModel;
use Illuminate\Support\Facades\Request;


/**
 * 用户新闻列表
 * Class DbGoodsRepository
 * @package App\Shop\Repositories
 */
class DbArticleRepository implements ArticleInterface
{
    /**
     * 新闻模型
     * @return ArticleModel
     */
    public function getArticleModel()
    {
        return app()->make(ArticleModel::class);
    }


    /**
     * 评论模型
     * @return InfoCommentsModel
     */
    public function getInfoCommentsModel(){
        return app()->make(InfoCommentsModel::class);
    }

    /**
     * 文章收藏模型
     * @return InfoCollectionModel
     */
    public function getInfoCollectionModel(){
        return app()->make(InfoCollectionModel::class);
    }

    /**
     * 新闻分类模型
     * @return ArticleClassModel
     */
    public function getArticleClassModel()
    {
        return app()->make(ArticleClassModel::class);
    }

    /**
     * 获取文章列表
     * @param $where
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getArticleList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return  $this->getArticleModel()->newQuery()->with("user")->select($select)->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 数据保存
     * @param $data
     */
    public function saveData($data)
    {
        if (empty($data['status'])) {
            $data['status'] = 0;
        }
        $data["uid"] = UID;
        return $this->getArticleModel()->newQuery()->updateOrCreate(["id" => $data['id']], $data);
    }

    /**
     * 获取详细
     * @param $id
     * @return mixed|void
     */
    public function getArticleInfo($where){
        $request = Request::instance();
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $select1=empty($request->input("select1")) ? '*' : explode(",",$request->input("select1"));
        $select2=empty($request->input("select1")) ? '*' : explode(",",$request->input("select2"));
        $this->getArticleModel()->newQuery()->increment("behot_time");
        return $this->getArticleModel()->newQuery()->select($select)
            ->with([
                "comments"=>
                    function($query) use ($select1,$limit,$sort,$order,$select2){
                        $query->select($select1);
                        $query->with(["user"=>function($query) use ($select2){
                           $query->select($select2);
                        }]);
                        $query->orderBy($sort, $order);
                        $query->paginate($limit);
                    }
        ])->where($where)->first();
    }

    public function getArticleCollectionInfo($where){

        $request = Request::instance();
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $select1=empty($request->input("select1")) ? '*' : explode(",",$request->input("select1"));
        $select2=empty($request->input("select1")) ? '*' : explode(",",$request->input("select2"));

        $this->getArticleModel()->newQuery()->increment("behot_time");
        return $this->getArticleModel()->newQuery()->select($select)
            ->with([
                "collection"=>
                    function($query) use ($select1,$limit,$sort,$order,$select2){
                        $query->select($select1);
                        $query->with(["user"=>function($query) use ($select2){
                            $query->select($select2);
                        }]);
                        $query->orderBy($sort, $order);
                        $query->paginate($limit);
                    }
              ])->where($where)->first();
    }

    /**
     *
     */
    public function getArticleChannel($where){
        $request = Request::instance();
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
         return $this->getArticleClassModel()->newQuery()->select($select)->where($where)->orderBy($sort, $order)->get();
    }

    /**
     * 文章评论
     * @param $data
     * @return mixed|void
     */
    public function setArticleComment($data){
      $data["user_id"]=AUID;
      if($this->getInfoCommentsModel()->newQuery()->create($data)){
          return  $this->getArticleModel()->newQuery()->increment("comment_count");
      }
    }


    /**
     * 文章收藏
     * @param $data
     * @return mixed|void
     */
    public function setArticleCollection($data){
        $data["user_id"]=AUID;
        $info=$this->getInfoCollectionModel()->newQuery()->where($data)->exists();
        if($info){
            return $this->getInfoCollectionModel()->newQuery()->where($data)->delete();
        }
        if($this->getInfoCollectionModel()->newQuery()->create($data)){
           return  $this->getArticleModel()->newQuery()->increment("collection_count");
        }
    }
    /**
     * 删除数据
     * @param $id
     * @return mixed
     */
    public function deleteData($where)
    {
        return $this->getArticleModel()->newQuery()->where($where)->delete();
    }
}